/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.loopj.hos.image.data;

import com.loopj.hos.image.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

import java.util.ArrayList;

public class ListItemProvider extends RecycleItemProvider {
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "ListItemProvider");
    private AbilitySlice mSlice;
    private OnItemClickListener listener;
    private ArrayList<DataMo> dataMos = new ArrayList<>();
    private ArrayList<Uri> urlList = new ArrayList<>();

    public ListItemProvider(AbilitySlice abilitySlice, OnItemClickListener listener) {
        this.mSlice = abilitySlice;
        this.listener = listener;
    }

    public void setData(ArrayList<PixelMap> pictureMos, ArrayList<Uri> list) {
        this.dataMos.clear();
        urlList.clear();
        urlList.addAll(list);
        int i = 0;
        ArrayList<PixelMap> tempList = new ArrayList<>();
        for (PixelMap mo : pictureMos) {
            if (i == 3) {
                i = 0;
                dataMos.add(new DataMo(tempList));
                tempList = new ArrayList<>();
            }
            tempList.add(mo);
            i++;
        }
        dataMos.add(new DataMo(tempList));
        this.notifyDataChanged();
    }

    @Override
    public int getCount() {
        return dataMos.size();
    }

    @Override
    public Object getItem(int i) {
        return dataMos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component component_item = LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_list_item, null, false);
        if (!(component_item instanceof ComponentContainer)) {
            return null;
        }
        ComponentContainer rootLayout = (ComponentContainer) component_item;
        DataMo dataMo = dataMos.get(i);
        for (int j = 0; j < dataMo.pictureMos.size(); j++) {
            Image imageItem = (Image) LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_item_title, null, false);
            imageItem.setPixelMap(dataMo.pictureMos.get(j));
            imageItem.setComponentDescription(j + "");
            rootLayout.addComponent(imageItem);
            imageItem.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    int index = Integer.parseInt(component.getComponentDescription().toString());
                    listener.OnItemClick(urlList.get(index), index + (i * 3));
                }
            });
        }
        return component_item;
    }

    public interface OnItemClickListener {
        void OnItemClick(Uri uri, int position);
    }
}
